package com.hoow.shakoow.shake.adManager

import android.graphics.drawable.ColorDrawable
import androidx.core.content.ContextCompat
import com.google.android.ads.nativetemplates.NativeTemplateStyle
import com.google.android.ads.nativetemplates.TemplateView
import com.google.android.gms.ads.AdListener
import com.google.android.gms.ads.AdLoader
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.LoadAdError
import com.hoow.shakoow.R
import com.hoow.shakoow.base.BaseActivity

class NativeAdManager(val activity: BaseActivity) {

    // 广告归属id
    // private val mNativeAdUnitId = "ca-app-pub-3940256099942544/2247696110"

    // 加载广告
    fun showAds(templateView: TemplateView) {
        val natId = AdsIdUtil.getNatUnitId()
        val adLoader: AdLoader = AdLoader.Builder(activity, natId)
            .forNativeAd { nativeAd ->
                println("===> forNativeAd nativeAd")
                val colorDrawable = ColorDrawable(
                    ContextCompat.getColor(
                        activity,
                        R.color.background_form_input
                    )
                )
                val styles =
                    NativeTemplateStyle.Builder().withMainBackgroundColor(colorDrawable).build()
                templateView.setStyles(styles)
                templateView.setNativeAd(nativeAd)
            }
            .withAdListener(object : AdListener() {
                override fun onAdClicked() {
                    super.onAdClicked()
                    println("===> onAdClicked")
                }

                override fun onAdClosed() {
                    super.onAdClosed()
                    println("===> onAdClosed")
                }

                override fun onAdFailedToLoad(p0: LoadAdError) {
                    super.onAdFailedToLoad(p0)
                    println("===> onAdFailedToLoad")
                }

                override fun onAdImpression() {
                    super.onAdImpression()
                    println("===> onAdImpression")
                }

                override fun onAdLoaded() {
                    super.onAdLoaded()
                    println("===> onAdLoaded")
                }

                override fun onAdOpened() {
                    super.onAdOpened()
                    println("===> onAdOpened")
                }

                override fun onAdSwipeGestureClicked() {
                    super.onAdSwipeGestureClicked()
                    println("===> onAdSwipeGestureClicked")
                }
            })
            .build()

        adLoader.loadAd(AdRequest.Builder().build())
    }
}